package cn.iocoder.yudao.module.brokerage.user.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.brokerage.user.pojo.bo.BrokerageUserMonthSummaryBO;
import cn.iocoder.yudao.module.brokerage.user.pojo.vo.AppBrokerageUserSimpleVO;
import cn.iocoder.yudao.module.brokerage.user.pojo.vo.AppBrokerageUserSummaryRespVO;
import cn.iocoder.yudao.module.brokerage.user.pojo.vo.AppBrokerageUserVO;
import cn.iocoder.yudao.module.brokerage.user.service.AppBrokerageUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 推广用户")
@RestController
@RequestMapping("/brokerage/user")
@Validated
public class AppBrokerageUserController {

    @Resource
    private AppBrokerageUserService appBrokerageUserService;

    @GetMapping("/my")
    @Operation(summary = "获得我的推广详情")
    public CommonResult<AppBrokerageUserVO> getMyDetail() {
        return success(appBrokerageUserService.getDetailByUserId(getLoginUserId()));
    }

    @GetMapping("/get-simple")
    @Operation(summary = "获得指定用户的推广用户")
    @Parameter(name = "userId", description = "用户编号", required = true, example = "1")
    @PermitAll
    public CommonResult<AppBrokerageUserSimpleVO> getSimple(@RequestParam(value = "userId") Long userId) {
        AppBrokerageUserVO respVO = appBrokerageUserService.getDetailByUserId(userId);
        return success(BeanUtils.toBean(respVO, AppBrokerageUserSimpleVO.class));
    }

    @GetMapping("/get-summary")
    @Operation(summary = "获得我的推广数据汇总")
    public CommonResult<AppBrokerageUserSummaryRespVO> getSummary() {
        return success(appBrokerageUserService.getSummaryByUserId(getLoginUserId()));
    }

    @GetMapping("/get-summary-by-month")
    @Operation(summary = "获得我的推广数据汇总（按月）")
    @Parameter(name = "year", description = "年份", example = "2025")
    @Parameter(name = "month", description = "月份", example = "1")
    public CommonResult<BrokerageUserMonthSummaryBO> getSummaryByMonth(@RequestParam(value = "year", required = false) Integer year, @RequestParam(value = "month", required = false) Integer month) {
        if (year == null || year <= 2024) {
            year = LocalDate.now().getYear();
        }
        if (month == null || month <= 0) {
            month = LocalDate.now().getMonth().getValue();
        }
        return success(appBrokerageUserService.getSummaryByUserIdAndMonth(getLoginUserId(), year, month));
    }

    @GetMapping("/get-summary-by-last-half-year")
    @Operation(summary = "获得我的推广数据汇总（近半年）")
    public CommonResult<List<BrokerageUserMonthSummaryBO>> getSummaryByLastHalfYear() {
        return success(appBrokerageUserService.getByUserIdAndLastHalfYear(getLoginUserId()));
    }

    @GetMapping("/page-father-grandpa")
    @Operation(summary = "获得我的直推及间推用户分页")
    @Parameter(name = "pageNo", description = "页码", required = true, example = "1")
    @Parameter(name = "pageSize", description = "每页数据条数", required = true, example = "10")
    public CommonResult<PageResult<AppBrokerageUserVO>> getFatherAndGrandpaPage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return success(appBrokerageUserService.getFatherAndGrandpaPage(pageNo, pageSize, getLoginUserId()));
    }

    @GetMapping("/page-father")
    @Operation(summary = "获得我的直推用户分页")
    @Parameter(name = "pageNo", description = "页码", required = true, example = "1")
    @Parameter(name = "pageSize", description = "每页数据条数", required = true, example = "10")
    public CommonResult<PageResult<AppBrokerageUserVO>> getFatherPage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return success(appBrokerageUserService.getFatherPage(pageNo, pageSize, getLoginUserId()));
    }

    @GetMapping("/page-grandpa")
    @Operation(summary = "获得我的间推用户分页")
    @Parameter(name = "pageNo", description = "页码", required = true, example = "1")
    @Parameter(name = "pageSize", description = "每页数据条数", required = true, example = "10")
    public CommonResult<PageResult<AppBrokerageUserVO>> getGrandpaPage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return success(appBrokerageUserService.getGrandpaPage(pageNo, pageSize, getLoginUserId()));
    }

    @GetMapping("/list-by-month")
    @Operation(summary = "获得我的月度直推用户列表")
    @Parameter(name = "year", description = "年份", required = true, example = "2025")
    @Parameter(name = "month", description = "月份", required = true, example = "1")
    public CommonResult<List<AppBrokerageUserVO>> getFatherList(@RequestParam(value = "year", defaultValue = "1") Integer year, @RequestParam(value = "month", defaultValue = "1") Integer month) {
        return success(appBrokerageUserService.getFatherListByMonth(getLoginUserId(), year, month));
    }

}